@echo off
chcp 65001 >nul
setlocal enabledelayedexpansion

echo.
echo ════════════════════════════════════════════
echo   快速诊断并修复
echo ════════════════════════════════════════════
echo.

:MENU
echo.
echo 请选择操作：
echo.
echo   [1] 完整诊断（查看详细状态）
echo   [2] 快速修复（重启应用 + 清理服务）
echo   [3] 仅重启应用
echo   [4] 仅停止融合定位服务
echo   [5] 查看当前位置
echo   [6] 查看实时日志
echo   [0] 退出
echo.
set /p choice="请输入选项 (0-6): "

if "%choice%"=="1" goto DIAGNOSE
if "%choice%"=="2" goto QUICK_FIX
if "%choice%"=="3" goto RESTART_APP
if "%choice%"=="4" goto STOP_SERVICES
if "%choice%"=="5" goto VIEW_LOCATION
if "%choice%"=="6" goto VIEW_LOG
if "%choice%"=="0" goto END
goto MENU

:DIAGNOSE
echo.
echo ════════════════════════════════════════════
echo   完整诊断
echo ════════════════════════════════════════════
echo.
echo [1] 检查应用是否运行...
adb shell "ps -A | grep munidingwei"
echo.
echo [2] 检查前台服务...
adb shell "dumpsys activity services | grep LocationForegroundService"
echo.
echo [3] 检查 Mock Provider 状态...
adb logcat -d -s MockLocationProvider:D | findstr "Mock Location Provider设置完成" | more +1000
echo.
echo [4] 检查系统位置...
adb shell "dumpsys location | grep 'last location'"
echo.
echo [5] 检查是否有错误...
adb logcat -d -s MockLocationProvider:E LocationService:E
echo.
pause
goto MENU

:QUICK_FIX
echo.
echo ════════════════════════════════════════════
echo   快速修复
echo ════════════════════════════════════════════
echo.
echo [1/3] 重启应用...
adb shell am force-stop com.example.munidingwei
timeout /t 1 /nobreak >nul
adb shell am start -n com.example.munidingwei/.MainActivity
echo   ✓ 应用已重启
echo.
echo 等待初始化（5秒）...
timeout /t 5 /nobreak >nul
echo.
echo [2/3] 停止融合定位服务...
adb shell am force-stop com.autonavi.minimap
adb shell am force-stop com.baidu.BaiduMap
adb shell am force-stop com.google.android.gms
adb shell am force-stop com.xiaomi.location.fused
echo   ✓ 服务已停止
echo.
echo [3/3] 验证状态...
adb shell "dumpsys location | grep 'last location' | grep mock"
echo.
echo ✅ 修复完成！请在应用中重新设置模拟位置。
echo.
pause
goto MENU

:RESTART_APP
echo.
echo ════════════════════════════════════════════
echo   重启应用
echo ════════════════════════════════════════════
echo.
adb shell am force-stop com.example.munidingwei
timeout /t 1 /nobreak >nul
adb shell am start -n com.example.munidingwei/.MainActivity
echo.
echo ✅ 应用已重启，等待5秒初始化...
timeout /t 5 /nobreak >nul
echo.
pause
goto MENU

:STOP_SERVICES
echo.
echo ════════════════════════════════════════════
echo   停止融合定位服务
echo ════════════════════════════════════════════
echo.
adb shell am force-stop com.autonavi.minimap
echo   ✓ 高德地图
adb shell am force-stop com.baidu.BaiduMap
echo   ✓ 百度地图
adb shell am force-stop com.google.android.gms
echo   ✓ Google Play Services
adb shell am force-stop com.xiaomi.location.fused
echo   ✓ 小米定位服务
echo.
echo ✅ 所有服务已停止
echo.
pause
goto MENU

:VIEW_LOCATION
echo.
echo ════════════════════════════════════════════
echo   当前位置
echo ════════════════════════════════════════════
echo.
adb shell "dumpsys location | grep 'last location'"
echo.
pause
goto MENU

:VIEW_LOG
echo.
echo ════════════════════════════════════════════
echo   实时日志（按 Ctrl+C 停止）
echo ════════════════════════════════════════════
echo.
adb logcat -c
adb logcat -s MockLocationProvider:D LocationService:D LocationForegroundService:D
echo.
pause
goto MENU

:END
echo.
echo 再见！
echo.
exit /b 0

